package com.iris.live.services.data.repositories.impl;

import com.google.common.collect.Maps;
import com.iris.live.services.common.DateFormatUtils;
import com.iris.live.services.data.repositories.StockForecastRepository;
import org.springframework.stereotype.Component;

import java.util.Map;

import java.util.List;
import java.util.Map;

/**
 * Created by yitianle on 2017/9/1.
 */
@Component()
public class StockForecastRepositoryImpl  extends BaseRepository implements StockForecastRepository {

    //存储各种查询key值某些指标是运算的场景不用再次消耗查询性能
    private static  Map<String,Object> map = Maps.newHashMap();

    @Override
    public Integer getStockOnTheWay(String brand,String carClass,String carType, Integer queryType,String permit) {
        String sql = null ;
        String key = "onTheWayStock";
        switch (queryType){
            case 1:
                sql ="select count(s.`库存编号`) from `lsh库存表` as s \n" +
                        " LEFT JOIN `报价交车表` as d ON s.`车架号` = d.vin \n" +
                        " LEFT JOIN  `lsh车型` as ct ON s.`车型编号` = ct.`车型编号`\n" +
                        " WHERE  ct.`品牌`='" + brand +
                        "' AND\n ct.`级别` ='" + carClass +
                        "' AND\n ct.`车型` ='" + carType +
                        "' AND\n" +
                        "s.`批发日期` is NOT NULL\n" +
                        "AND\n" +
                        "s.`入库日期` is NULL\n" +
                        "AND\n" +
                        "d.`开票时间` is NULL" +
                        " AND\n" +
                        "s.`所属经销商` ='" + permit+"'";
                break;
            case 2:
                sql ="select count(s.`库存编号`) from `lsh库存表` as s \n" +
                        "LEFT JOIN `报价交车表` as d ON s.`车架号` = d.vin \n" +
                        "LEFT JOIN  `lsh车型` as ct ON s.`车型编号` = ct.`车型编号`\n" +
                        "WHERE  ct.`品牌`=" + "'"+brand +"'"+
                        " AND\n ct.`级别` =" +"'"+ carClass +"'"+
                        " AND\n ct.`车型` =" + "'"+carType +"'"+
                        " AND\n" +
                        " s.`批发日期` is NOT NULL\n" +
                        " AND\n" +
                        " s.`入库日期` is NOT NULL\n" +
                        " AND\n" +
                        " d.`开票时间` is NULL"+
                        " AND\n" +
                        " s.`所属经销商` =" +"'"+ permit+"'";
                key = "localStock";
                break;
        }
        Integer countNum = getCount(sql);
        map.put(key,countNum);
        return countNum;
    }

    @Override
    public Double getAvgStockTurnoverDays(String permit,String brand ,String  grade,String model) {
        String startDay = DateFormatUtils.getBeforeMonthFirstDay(1,3);
        String endDay = DateFormatUtils.getBeforeMonthLastDay(1,3);
        String sql = " select   a.许可, a.品牌, a.级别, a.车型,a.`实际库存`,IFNULL(b.前三个月平均销量   ,0) 前三个月平均销量 , ROUND(IFNULL(a.`实际库存`/IFNULL(b.前三个月平均销量   ,0),0),2) 库存周转天数  from   \n" +
                "    -- 实际库存\n" +
                "    (  select 30 * count(1) 实际库存, b.所属经销商 as 许可,品牌,级别 ,车型    \n" +
                "                        from  LSH车型 a, LSH库存表 b LEFT JOIN vw_retailed_quotation c ON c.库存编号 = b.库存编号     \n" +
                "                        WHERE a.车型编号 = b.车型编号 AND b.批发日期 <= "+"'"+ endDay +"'"+" and b.is_delete = 0 and b.车架号 not like 'IRIS%'    \n" +
                "                         and (case when 'MB' = '' then true else a.品牌 = 'MB' end)   \n" +
                "                         AND  (c.开票时间 IS NULL \n" +
                "                               OR (date_format(c.开票时间,'%Y-%m-%d') > "+ "'"+endDay +"'"+" and c.退票时间 IS NULL) \n" +
                "                               OR (date_format(c.开票时间,'%Y-%m-%d') > "+ "'"+endDay +"'"+" and c.退票时间 < c.开票时间) \n" +
                "                              ) \n" +
                "                        group by b.所属经销商,品牌,级别,车型     ) a\n" +
                "    -- 前三个月平均销量\n" +
                "    LEFT JOIN (\n" +
                "       select sum(CASE    \n" +
                "             WHEN date_format(a.开票时间,'%Y-%m-%d') >= CONCAT(DATE_FORMAT(DATE_SUB(" + "'"+ endDay +"'"+",INTERVAL 3 MONTH),'%Y-%m'),'-01')       \n" +
                "              and date_format(a.开票时间,'%Y-%m-%d')  <= DATE_FORMAT(last_day(DATE_SUB("+ "'"+ endDay +"'"+",INTERVAL 1 MONTH)),'%Y-%m-%d')    \n" +
                "              AND  date_format(a.退票时间,'%Y-%m-%d') >= CONCAT(DATE_FORMAT(DATE_SUB("+ "'"+ endDay +"'"+",INTERVAL 3 MONTH),'%Y-%m'),'-01')     \n" +
                "              and date_format(a.退票时间,'%Y-%m-%d')  <= DATE_FORMAT(last_day(DATE_SUB("+ "'"+ endDay +"'"+",INTERVAL 1 MONTH)),'%Y-%m-%d')    \n" +
                "              AND a.退票时间 > a.开票时间 THEN 0   \n" +
                "             WHEN date_format(a.开票时间,'%Y-%m-%d') >= CONCAT(DATE_FORMAT(DATE_SUB("+  "'"+endDay +"'"+",INTERVAL 3 MONTH),'%Y-%m'),'-01')  \n" +
                "              and date_format(a.开票时间,'%Y-%m-%d') <= DATE_FORMAT(last_day(DATE_SUB("+ "'"+ endDay +"'"+",INTERVAL 1 MONTH)),'%Y-%m-%d') THEN 1   \n" +
                "             WHEN date_format(a.退票时间,'%Y-%m-%d') >= CONCAT(DATE_FORMAT(DATE_SUB("+ "'"+ endDay +"'"+",INTERVAL 3 MONTH),'%Y-%m'),'-01')       \n" +
                "              and  date_format(a.退票时间,'%Y-%m-%d') <= DATE_FORMAT(last_day(DATE_SUB("+ "'"+ endDay +"'"+",INTERVAL 1 MONTH)),'%Y-%m-%d') THEN -1   \n" +
                "             END) / 3 as '前三个月平均销量', a.许可 ,e.品牌,e.级别,e.车型       \n" +
                "        from vw_retailed_quotation a          \n" +
                "        join LSH车型 e on a.车型编号 = e.车型编号   \n" +
                "        join 报价订单表 c ON a.订单编号 = c.订单编号  \n" +
                "        where  ((date_format(a.开票时间,'%Y-%m-%d') <= DATE_FORMAT(last_day(DATE_SUB("+ "'"+ endDay +"'"+",INTERVAL 1 MONTH)),'%Y-%m-%d')  AND date_format(a.开票时间,'%Y-%m-%d') >= CONCAT(DATE_FORMAT(DATE_SUB("+  endDay +",INTERVAL 3 MONTH),'%Y-%m'),'-01'))      \n" +
                "           OR (date_format(a.退票时间,'%Y-%m-%d') <= DATE_FORMAT(last_day(DATE_SUB("+ "'"+ endDay +"'"+",INTERVAL 1 MONTH)),'%Y-%m-%d')  AND date_format(a.退票时间,'%Y-%m-%d') >= CONCAT(DATE_FORMAT(DATE_SUB("+  endDay +",INTERVAL 3 MONTH),'%Y-%m'),'-01')))  \n" +
                "         AND a.订单编号 = c.订单编号  \n" +
                "         AND c.销售类型 not like 'B%'  \n" +
                "         AND c.`销售类型`not like 'I%'      \n" +
                "         and a.车辆零售状态 = '已卖'      \n" +
                "         and (case when 'MB' = '' then true else e.品牌 = 'MB' end)       \n" +
                "        group by a.许可,e.品牌,e.级别,e.车型      \n" +
                "    ) b on a.许可=b.许可   and a.品牌= b.品牌 and a.级别 =b.级别  and a.车型=b.车型   \n" +
                "    where a.许可=:permit   and a.品牌=:brand and a.级别 =:grade  and a.车型=:model  ";
        Map params = Maps.newHashMap();
        params.put("permit", permit);
        params.put("brand", permit);
        params.put("grade", permit);
        params.put("model", permit);
        List result=this.getList(sql, params);
        Double data=0d;
        if(result!=null&&result.size()!=0){
            data=Double.parseDouble(((Map)result.get(0)).get("库存周转天数").toString());
        }
        return data;
    }

    @Override
    public Integer getSaleTarget(String brand,String carClass,String carType,String permit) {
        String sql = "select count(s.LSH_TA) from snp_setting as s " +
                " LEFT JOIN  `lsh车型` as ct ON s.`carCode` = ct.`车型编号`\n" +
                " WHERE  ct.`品牌`=" +"'"+ brand +"'"+
                " AND\n ct.`级别` =" + "'"+carClass +"'"+
                " AND\n ct.`车型` =" + "'"+carType +"'"+
                " AND\n s.`permit` =" +"'"+ permit +"'"+
                " AND s.y_m=" + DateFormatUtils.getLastMonth(1);
        Integer countNum = getCount(sql);
        map.put("saleTarget",countNum);
        return countNum;
    }

    @Override
    public Integer getClueStockNum(String brand,String carClass,String carType,String permit) {
        String sql ="select COUNT(1) from `顾客记录汇总` as c \n" +
                "INNER  JOIN `顾客进店记录表` as j ON c.`顾客编号` = j.顾客编号\n" +
                "INNER JOIN `报价单表`  as q ON  c.`顾客编号` =q.`顾客编号`\n" +
                "left JOIN `lsh车型`  as t ON  j.`意向车型A` =t.`车型编号`\n" +
                " WHERE\n" +
                " c.`许可` = "+"'"+permit+"'"+"\n" +
                " AND\n" +
                " t.`品牌` = " +"'"+ brand +"'"+
                " and t.`级别` =" + "'"+carClass +"'"+
                " and t.`车型` =" + "'"+carType +"'"+
                " And\n" +
                "j.`到店时间` != '01:00:00'" +
                " and q.`订单编号` is NULL ";
        Integer countNum = getCount(sql);
        map.put("clueStockNum",countNum);
        return countNum;
    }

    /**
     * 获取增量线索 意向车型匹配，有效期内（前2个月内）未进店线索+过去30天（上个月）新增线索数
     * @param brand
     * @param carClass
     * @param carType
     * @return
     */
    @Override
    public Integer getClueAddNum(String brand,String carClass,String carType,String permit) {
        String startDay = DateFormatUtils.getBeforeMonthFirstDay(2,3);
        String endDay = DateFormatUtils.getBeforeMonthLastDay(2,3);
        String startDayLast = DateFormatUtils.getBeforeMonthFirstDay(1,3);
        String endDayLast = DateFormatUtils.getBeforeMonthLastDay(1,3);
        String sqlReportNameList = "SELECT count(1) from `lsh导入名单` as d \n" +
                "LEFT JOIN `顾客记录汇总` as u ON d.`电话号码` = u.`电话号码`\n" +
                "LEFT JOIN `lsh车型` as t  ON d.`车型编号` = t.`车型编号`\n" +
                "WHERE\n" +
                "d.`许可` =" + "'"+permit +"'"+
                " AND\n" +
                " u.顾客编号 is NULL\n" +
                " AND\n" +
                " d.`线索创建日期` >=" + "'"+startDay +"'"+
                " AND\n" +
                " d.`线索创建日期` <=" +"'"+ endDay +"'"+
                " AND\n" +
                " t.`品牌` =" + "'"+brand +"'"+
                " AND\n" +
                " t.`级别` =" + "'"+carClass +"'"+
                " AND\n" +
                " t.车型 =" +"'"+ carType+"'";

        String sqlNotStore="SELECT count(1) from `顾客记录汇总` as u \n" +
                "LEFT JOIN `顾客进店记录表` as s ON u.`顾客编号` = s.`顾客编号`\n" +
                "LEFT JOIN `lsh车型` as t  ON s.`意向车型A` = t.`车型编号`\n" +
                "WHERE\n" +
                " u.`许可` =" + "'"+permit +"'"+
                " AND\n" +
                " u.`留档日期` >=" +"'"+ startDay +"'"+
                " AND\n" +
                " u.`留档日期` <=" + "'"+endDay +"'"+
                " AND\n" +
                " t.`品牌` =" +"'"+ brand +"'"+
                " AND\n" +
                " t.`级别` =" +"'"+ carClass +"'"+
                " AND\n" +
                " t.车型 =" + "'"+carType +"'"+
                " AND\n" +
                " s.`到店时间` ='01:00:00'";

        String sqlLastAddClue="select count(1) from (select d.`电话号码`,d.`许可`,d.`线索创建日期`as startDate,d.`车型编号`  from `lsh导入名单` as d where d.`线索创建日期` >="+ "'"+startDayLast +"'"+" and d.`线索创建日期` <="+ "'"+endDayLast +"'"+"\n" +
                "UNION \n" +
                "select c.`电话号码`,c.`许可`,c.`留档日期`as startDate,c.`意向车型A` as 车型编号 from `顾客记录汇总` as c where c.`留档日期` >="+"'"+ startDayLast +"'"+" and c.`留档日期` <="+ "'"+endDayLast +"'"+") as a\n" +
                "LEFT JOIN `lsh车型` as t ON a.`车型编号` = t.`车型编号`\n" +
                "WHERE\n" +
                " a.`许可` =" +"'"+ permit +"'"+
                " AND \n" +
                " t.`品牌` =" +"'"+ brand +"'"+
                " AND\n" +
                " t.`级别` =" +"'"+ carClass +"'"+
                " AND\n" +
                " t.`车型` =" +"'"+ carType+"'" ;
        Integer  value = getCount(sqlReportNameList) + getCount(sqlNotStore) + getCount(sqlLastAddClue);
        map.put("addClueNum",value);
        return value;
    }

    /**
     * 线索进店率
     * @param brand
     * @param carClass
     * @param carType
     * @param permit
     * @return
     */
    @Override
    public String getClueStoreRate(String brand,String carClass,String carType,String permit) {
        String startDay = DateFormatUtils.getBeforeMonthFirstDay(3,3);
        String endDay = DateFormatUtils.getBeforeMonthLastDay(3,3);
        //前三个月总线索数
        String sqlAll ="select count(1) from (select d.`电话号码`,d.`许可`,d.`线索创建日期`as startDate,d.`车型编号`  from `lsh导入名单` as d where d.`线索创建日期` >="+ "'"+startDay +"'"+" and d.`线索创建日期` <="+"'"+ endDay +"'"+"\n" +
                "UNION \n" +
                "select c.`电话号码`,c.`许可`,c.`留档日期`as startDate,c.`意向车型A` as 车型编号 from `顾客记录汇总` as c where c.`留档日期` >="+"'"+ startDay +"'"+" and c.`留档日期` <="+"'"+ endDay +"'"+") as a\n" +
                "LEFT JOIN `lsh车型` as t ON a.`车型编号` = t.`车型编号`\n" +
                "WHERE\n" +
                " a.`许可` =" +"'"+ permit +"'"+
                " AND \n" +
                " t.`品牌` =" + "'"+brand +"'"+
                " AND\n" +
                " t.`级别` =" + "'"+carClass +"'"+
                " AND\n" +
                " t.`车型` =" +"'"+ carType+"'" ;

        //前三个月进店线索数
        String sqlStore ="select COUNT(1) from `顾客记录汇总` as c \n" +
                "INNER  JOIN `顾客进店记录表` as j ON c.`顾客编号` = j.顾客编号\n" +
                "left JOIN `lsh车型`  as t ON  j.`意向车型A` =t.`车型编号`\n" +
                " WHERE\n" +
                " c.`许可` =" +"'"+ permit +"'"+
                " AND\n" +
                " t.`品牌` =" +"'"+ brand +"'"+
                " AND\n" +
                " t.`级别` =" +"'"+ carClass +"'"+
                " AND\n" +
                " t.`车型` =" + "'"+carType +"'"+
                " AND\n" +
                " c.`留档日期` >=" +"'"+ startDay +"'"+
                " AND\n" +
                " c.`留档日期` <=" +"'"+ endDay +"'"+
                " AND\n" +
                " j.`到店时间` != '01:00:00'";

        Integer countAllNum = getCount(sqlAll);
        Integer countStoreNum = getCount(sqlStore);
        Double value = (countStoreNum / countAllNum.doubleValue()) / 3;
        if(value.isNaN()||value.isInfinite()){
            value = 0.0;
        }
        map.put("clueStoreRate",value);
        return value +"";
    }

    @Override
    public String getClueStoreOrderNum(String brand,String carClass,String carType,String permit) {
        String startDay = DateFormatUtils.getBeforeMonthFirstDay(3,3);
        String endDay = DateFormatUtils.getBeforeMonthLastDay(3,3);
        //获取当前车型进店线索数
        String sqlAllStoreClue = "select count(1) from `顾客记录汇总` as u \n" +
                "LEFT JOIN `顾客进店记录表` as j ON u.`顾客编号` = j.`顾客编号`\n" +
                "LEFT JOIN `lsh车型` as t ON j.`意向车型A`  = t.车型编号\n" +
                "WHERE\n" +
                " j.`到店时间` !='01:00:00'\n" +
                " AND\n" +
                " u.`许可` =" + "'"+permit +"'"+
                " AND\n" +
                " u.`留档日期` >=" +"'"+ startDay +"'"+
                " AND\n" +
                " u.`留档日期` <=" +"'"+ endDay +"'"+
                " AND\n" +
                " t.`品牌`=" +"'"+ brand +"'"+
                " AND\n t.`级别` =" +"'"+ carClass +"'"+
                " AND\n t.`车型` =" + "'"+carType+"'" ;
        //进店线索订单数
        String sqlStoreClueOrder ="select count(1) from `顾客记录汇总` as u \n" +
                "LEFT JOIN `顾客进店记录表` as j ON u.`顾客编号` = j.`顾客编号`\n" +
                "LEFT JOIN `lsh车型` as t ON j.`意向车型A`  = t.车型编号\n" +
                "LEFT JOIN `报价单表` as q ON u.`顾客编号` = q.`顾客编号`\n" +
                "LEFT JOIN `报价订单表` as o ON q.`订单编号` = o.`订单号`\n" +
                "WHERE\n" +
                " j.`到店时间` !='01:00:00'\n" +
                " AND\n" +
                " u.`许可` =" +"'"+ permit +"'"+
                " AND\n" +
                " u.`留档日期` >=" + "'"+startDay +"'"+
                " AND\n" +
                " u.`留档日期` <=" +"'"+ endDay +"'"+
                " AND\n" +
                " t.`品牌`=" +"'"+ brand +"'"+
                " AND\n t.`级别` =" + "'"+carClass +"'"+
                " AND\n t.`车型` =" + "'"+carType +"'"+
                " AND\n" +
                " o.`订单审批通过日期` is NOT NULL\n" +
                " AND\n" +
                " o.`订单失销日期` is NULL";
        Integer countStoreClueNum = getCount(sqlAllStoreClue);
        Integer countStoreClueOrderNum = getCount(sqlStoreClueOrder);
        Double value = countStoreClueOrderNum / countStoreClueNum.doubleValue();
        if(value.isNaN()||value.isInfinite()){
            value = 0.0;
        }
        map.put("clueStoreOrderRate",value);
        return value +"";
    }

    @Override
    public Integer getStockOrderNum(String brand,String carClass,String carType,String permit) {
        String sql ="select COUNT(1) from (select DISTINCT(q.`初始报价单号`) from `报价单表` as q  \n" +
                " INNER JOIN `报价订单表` as o ON q.`订单编号` = o.`订单编号`\n" +
                " left JOIN  `报价交车表` as c ON q.`交车` = c.`编号`\n" +
                " left JOIN  `lsh车型` as ct ON q.`车型编号` = ct.`车型编号`\n" +
                " WHERE  ct.`品牌`=" +"'"+ brand +"'"+
                " AND ct.`级别` =" + "'"+carClass +"'"+
                " AND ct.`车型` =" + "'"+carType +"'"+
                " AND o.`订单审批通过日期` >='" + DateFormatUtils.getBeforeMonthFirstDay(1,1) +"'"+
                " AND o.`订单审批通过日期` <='" + DateFormatUtils.getBeforeMonthLastDay(1,1) +"'"+
                " AND q.`许可` =" +"'"+ permit +"'"+
                " AND c.`开票时间` is NULL\n" +
                " AND o.`订单失销日期` is NULL\n" +
                " GROUP BY q.`初始报价单号`) a";
        Integer countNum = getCount(sql);
        map.put("stockOrderNum",countNum);
        return countNum;
    }

    @Override
    public String getOrderLeaveCarNum(String brand,String carClass,String carType,String permit) {
        //前三个月的订单数
        String sqlOrder ="select COUNT(1) from (select DISTINCT(q.`初始报价单号`) from `报价单表` as q  \n" +
                " INNER JOIN `报价订单表` as o ON q.`订单编号` = o.`订单编号`\n" +
                " left JOIN  `lsh车型` as ct ON q.`车型编号` = ct.`车型编号`\n" +
                " WHERE  ct.`品牌`=" + "'"+brand +"'"+
                " AND ct.`级别` =" + "'"+carClass +"'"+
                " AND ct.`车型` =" + "'"+carType +"'"+
                " AND o.`订单审批通过日期` >='" + DateFormatUtils.getBeforeMonthFirstDay(3,1) +"'"+
                " AND o.`订单审批通过日期` <='" + DateFormatUtils.getBeforeMonthLastDay(3,1) +"'"+
                " AND q.`许可` =" +"'"+ permit +"'"+
                " AND o.`订单失销日期` is NULL\n" +
                " GROUP BY q.`初始报价单号`) a";

        //前三个月的订单交车数
        String sqlLeave ="select COUNT(1) from (select DISTINCT(q.`初始报价单号`) from `报价单表` as q  \n" +
                " INNER JOIN `报价订单表` as o ON q.`订单编号` = o.`订单编号`\n" +
                " left JOIN  `报价交车表` as c ON q.`交车` = c.`编号`\n" +
                " left JOIN  `lsh车型` as ct ON q.`车型编号` = ct.`车型编号`\n" +
                " WHERE  ct.`品牌`=" +"'"+ brand +"'"+
                " AND ct.`级别` =" + "'"+carClass +"'"+
                " AND ct.`车型` =" +"'"+ carType +"'"+
                " AND o.`订单审批通过日期` >='" + DateFormatUtils.getBeforeMonthFirstDay(1,1) +"'"+
                " AND o.`订单审批通过日期` <='" + DateFormatUtils.getBeforeMonthLastDay(1,1) +"'"+
                " AND q.`许可` =" +"'"+ permit +"'"+
                " AND c.`交车日期` is NULL\n" +
                "  AND o.`订单失销日期` is NULL\n" +
                " GROUP BY q.`初始报价单号`) a";

        Integer countOrderNum = getCount(sqlOrder);
        Integer countLeaveNum = getCount(sqlLeave);
        Double value = countLeaveNum / countOrderNum.doubleValue();
        if(value.isNaN()||value.isInfinite()){
            value = 0.0;
        }
        map.put("orderLeaveCarRate",value);
        return value +"";
    }

    /**
     * 未分配资源(无批发日期的资源（未开票）)
     * @param brand
     * @param carClass
     * @param carType
     * @param permit
     * @return
     */
    @Override
    public Integer undistributedSource(String brand, String carClass, String carType, String permit) {
        String  sql ="select count(s.`库存编号`) from `lsh库存表` as s \n" +
                " LEFT JOIN `报价交车表` as d ON s.`车架号` = d.vin \n" +
                " LEFT JOIN  `lsh车型` as ct ON s.`车型编号` = ct.`车型编号`\n" +
                " WHERE  ct.`品牌`=" + "'"+brand +"'"+
                " AND\n ct.`级别` =" +"'"+ carClass +"'"+
                " AND\n ct.`车型` =" +"'"+ carType +"'"+
                " AND\n" +
                " s.`批发日期` is NULL\n" +
                " AND\n" +
                " d.`开票时间` is NULL"+
                " AND\n" +
                " s.`所属经销商` =" + "'"+permit+"'";
        Integer value = getCount(sql);
        map.put("undistributedSource",value);
        return value;
    }

    //以下为直接计算部分
    @Override
    public Integer getClueStoreNum() {
        Double value = 0.0;
        //进店线索数	（存量线索+增量线索）*线索进店率
        Integer  clueStoreMap  = Integer.parseInt(String.valueOf(map.get("clueStockNum")));
        Integer  clueAddMap  = Integer.parseInt(String.valueOf(map.get("addClueNum")));
        Double  clueStoreRateMap  = Double.parseDouble(String.valueOf(map.get("clueStoreRate")));
        value = (clueStoreMap + clueAddMap ) * clueStoreRateMap;
        map.put("clueStoreNum",value);
        return value.intValue();
    }

    @Override
    public Integer getAddOrderNum() {
        Double value = 0.0;
        //新增订单 进店线索数*进店线索订单率
        Double  clueStoreMap  = Double.parseDouble(String.valueOf(map.get("clueStoreNum")));
        Double  clueStoreOrderRateMap  = Double.parseDouble(String.valueOf(map.get("clueStoreOrderRate")));
        value =  clueStoreMap * clueStoreOrderRateMap;
        map.put("addOrderNum",value);
        return value.intValue();
    }

    @Override
    public Integer saleEstimate() {
        Double value = 0.0;
        //销售预计	（新增订单+存量订单）*订单交车率
        Double  addOrderMap  = Double.parseDouble(String.valueOf(map.get("addOrderNum")));
        Double  clueAddMap  = Double.parseDouble(String.valueOf(map.get("addClueNum")));
        Double  orderLeaveCarRate  = Double.parseDouble(String.valueOf(map.get("orderLeaveCarRate")));
        value = (addOrderMap + clueAddMap ) * orderLeaveCarRate;
        map.put("saleEstimate",value);
        return value.intValue();
    }

    @Override
    public Integer saleGap() {
        Double value = 0.0;
        //销售缺口	销售目标 -销售预计
        Double  saleTargetMap  = Double.parseDouble(String.valueOf(map.get("saleTarget")));
        Double  saleEstimateMap  = Double.parseDouble(String.valueOf(map.get("saleEstimate")));
        value =  saleTargetMap - saleEstimateMap;
        map.put("saleGap",value);
        return value.intValue();
    }

    @Override
    public Integer stockGap() {
        Double value = 0.0;
        //库存缺口	本店库存+在途库存 -销售预计
        Double  onTheWayStockMap  = Double.parseDouble(String.valueOf(map.get("onTheWayStock")));
        Double  localStockMap  = Double.parseDouble(String.valueOf(map.get("localStock")));
        Double  saleEstimateMap  = Double.parseDouble(String.valueOf(map.get("saleEstimate")));
        value =  onTheWayStockMap + localStockMap - saleEstimateMap;
        map.put("stockGap",value);
        return value.intValue();
    }

    @Override
    public Integer purchaseGap() {
        Double value = 0.0;
        //采购缺口	库存缺口+未分配资源
        Double  stockGapMap  = Double.parseDouble(String.valueOf(map.get("stockGap")));
        Double  undistributedSourceMap  = Double.parseDouble(String.valueOf(map.get("undistributedSource")));
        value =  stockGapMap + undistributedSourceMap;
        return value.intValue();
    }
}
